//
//  EditResumeAllViewController.m
//  CYWFrame
//
//  Created by 于顺燊 on 2018/9/9.
//  Copyright © 2018年 Macx. All rights reserved.
//

#import "EditResumeAllViewController.h"
#import "ResumeInfo.h"
#import "SDWebImage/UIButton+WebCache.h"
#import "AddWorkExpViewController.h"
#import "JobExpTableViewCell.h"
#import "AddEduExpViewController.h"
#import "EduExpTableViewCell.h"
#import "EditResumeBasicInfoViewController.h"
#import "EditWantJobViewController.h"
#import "ZYInputAlertView.h"
#import <TZImagePickerController.h>

@interface EditResumeAllViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UIView *contentView;
@property (weak, nonatomic) IBOutlet UILabel *ResumeName;
@property (weak, nonatomic) IBOutlet UIButton *headPic;
@property (weak, nonatomic) IBOutlet UIButton *editResumeTitleBtn;
@property (weak, nonatomic) IBOutlet UIButton *editBasicInfoBtn;
@property (weak, nonatomic) IBOutlet UIButton *editJobWantInfoBtn;
@property (weak, nonatomic) IBOutlet UIButton *editWorkExpBtn;
@property (weak, nonatomic) IBOutlet UIButton *editEduBtn;
@property (weak, nonatomic) IBOutlet UIButton *editMyAdvantageBtn;
@property (weak, nonatomic) IBOutlet UISwitch *privacySetSwitch;

//基本信息
@property (weak, nonatomic) IBOutlet UIView *BasicInfoView;
@property (weak, nonatomic) IBOutlet UILabel *myName;
@property (weak, nonatomic) IBOutlet UILabel *sex;
@property (weak, nonatomic) IBOutlet UILabel *ancestralHome;
@property (weak, nonatomic) IBOutlet UILabel *birthday;
@property (weak, nonatomic) IBOutlet UILabel *address;
@property (weak, nonatomic) IBOutlet UILabel *phone;
@property (weak, nonatomic) IBOutlet UILabel *email;
@property (weak, nonatomic) IBOutlet UILabel *workDate;
@property (weak, nonatomic) IBOutlet UILabel *desc;

//求职意向
@property (weak, nonatomic) IBOutlet UIView *positionView;
@property (weak, nonatomic) IBOutlet UILabel *jobWantState;
@property (weak, nonatomic) IBOutlet UILabel *salaryWant;
@property (weak, nonatomic) IBOutlet UILabel *postionWant;

//工作经历
@property (weak, nonatomic) IBOutlet UIView *workExpView;
@property (weak, nonatomic) IBOutlet UITableView *workExpTableView;
@property (weak, nonatomic) IBOutlet UIButton *addWorkExpBtn;

//教育背景
@property (weak, nonatomic) IBOutlet UIView *educationView;
@property (weak, nonatomic) IBOutlet UITableView *eduTableView;
@property (weak, nonatomic) IBOutlet UIButton *addEducationBtn;

//个人优势
@property (weak, nonatomic) IBOutlet UIView *advantageView;
@property (weak, nonatomic) IBOutlet UILabel *advantage;

//隐私设定
@property (weak, nonatomic) IBOutlet UIView *privacySetView;

@property (strong, nonatomic) ResumeInfo *resumeInfo;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *eduHeight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *workExpHeight;
@end

@implementation EditResumeAllViewController

- (void)viewWillAppear:(BOOL)animated
{
	[self getResumeInfo];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
	
	self.navigationItem.title = @"编辑简历";
	self.headPic.layer.cornerRadius = self.headPic.frame.size.height/2;
	self.headPic.clipsToBounds = YES;
	self.workExpTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
	[self.workExpTableView registerNib:[UINib nibWithNibName:@"JobExpTableViewCell" bundle:nil] forCellReuseIdentifier:@"jobCell"];
	self.workExpTableView.dataSource = self;
	self.workExpTableView.delegate = self;
	
	self.eduTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
	[self.eduTableView registerNib:[UINib nibWithNibName:@"EduExpTableViewCell" bundle:nil] forCellReuseIdentifier:@"eduCell"];
	self.eduTableView.dataSource = self;
	self.eduTableView.delegate = self;
	[self getResumeInfo];
	[self.addWorkExpBtn addTarget:self action:@selector(addWorkExp:) forControlEvents:UIControlEventTouchUpInside];
	[self.addEducationBtn addTarget:self action:@selector(addEduExp:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)updateViewConstraints
{
	[super updateViewConstraints];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)assignToUI
{
	self.ResumeName.text = self.resumeInfo.title;
	[self.ResumeName sizeToFit];
	[self.headPic sd_setBackgroundImageWithURL:[NSURL URLWithString:self.resumeInfo.head_img] forState:UIControlStateNormal];
	self.myName.text = self.resumeInfo.name;
	if ([self.resumeInfo.sex isEqualToString:@"1"]) {
		self.sex.text = @"男";
	}
	else {
		self.sex.text = @"女";
	}
	
	self.ancestralHome.text = self.resumeInfo.ancestral_home;
	
	self.birthday.text = @"";
	if (self.resumeInfo.birth_date.length >= 8) {
		NSString *yearStr = [self.resumeInfo.birth_date substringWithRange:NSMakeRange(0, 4)];
		NSString *monthStr = [self.resumeInfo.birth_date substringWithRange:NSMakeRange(4, 2)];
		NSString *dayStr = [self.resumeInfo.birth_date substringWithRange:NSMakeRange(6, 2)];
		self.birthday.text = [NSString stringWithFormat:@"%@年%@月%@日", yearStr, monthStr, dayStr];
	}
	
	self.address.text = [NSString stringWithFormat:@"%@ %@",self.resumeInfo.district, self.resumeInfo.address];
	self.phone.text = self.resumeInfo.mobile;
	self.email.text = self.resumeInfo.email;
	
	self.workDate.text = @"";
	if (self.resumeInfo.work_date.length >= 8) {
		NSString *yearStr2 = [self.resumeInfo.work_date substringWithRange:NSMakeRange(0, 4)];
		NSString *monthStr2 = [self.resumeInfo.work_date substringWithRange:NSMakeRange(4, 2)];
		NSString *dayStr2 = [self.resumeInfo.work_date substringWithRange:NSMakeRange(6, 2)];
		self.workDate.text = [NSString stringWithFormat:@"%@年%@月%@日", yearStr2, monthStr2, dayStr2];
	}
	
	self.desc.text = self.resumeInfo.desc;
	NSInteger statusNum = [self.resumeInfo.apply_status integerValue];
	if (statusNum == 0) {
		self.jobWantState.text = @"已离职";
	}
	else if(statusNum == 1){
		self.jobWantState.text = @"在职";
	}
	self.postionWant.text = self.resumeInfo.category_title;
	self.salaryWant.text = self.resumeInfo.salary;
	self.advantage.text = self.resumeInfo.superiority;
	[self.advantage sizeToFit];
	
}

//TableView delegate
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	if (tableView == self.workExpTableView) {
		JobExpTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"jobCell" forIndexPath:indexPath];
		cell.info = self.resumeInfo.work_list[indexPath.row];
		
		return cell;
	}
	else {
		EduExpTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"eduCell" forIndexPath:indexPath];
		cell.info = self.resumeInfo.educational_list[indexPath.row];
		return cell;
	}
	
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	if (tableView == self.workExpTableView) {
		AddWorkExpViewController *vc = [[AddWorkExpViewController alloc] init];
		vc.type = 1;
		vc.info = self.resumeInfo.work_list[indexPath.row];
		vc.resumeId = self.resumeId;
		[self.navigationController pushViewController:vc animated:YES];
		
	}
	else {
		AddEduExpViewController *vc = [[AddEduExpViewController alloc] init];
		vc.type = 1;
		vc.info = self.resumeInfo.educational_list[indexPath.row];
		vc.resumeId = self.resumeId;
		[self.navigationController pushViewController:vc animated:YES];
	}
	
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	if (tableView == self.workExpTableView) {
		return self.resumeInfo.work_list.count;
	}
	else {
		return self.resumeInfo.educational_list.count;
	}
	
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
	if (tableView == self.workExpTableView) {
		return 120;
	}
	else {
		return 120;
	}
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
	if (tableView == self.workExpTableView) {
		tableView.rowHeight = UITableViewAutomaticDimension;
		return tableView.rowHeight;
	}
	else {
		return 120;
	}
}

- (void)getResumeInfo
{
	//NSNumber *resumeIdNum = [NSNumber numberWithInteger:[self.resumeId integerValue]];
	NSDictionary *param = @{
							@"resume_id":self.resumeId
							};
	[[NetworkTool sharedTool] requestWithName:@"获取对应简历" URLString:APIGetResume parameters:param method:@"POST" callBack:^(id responseObject) {
		
		NSDictionary *dict = responseObject[@"data"];
		[self.resumeInfo setValuesForKeysWithDictionary:dict];
		[self assignToUI];
		[self.workExpTableView reloadData];
		self.workExpHeight.constant = self.workExpTableView.contentSize.height;
		[self.eduTableView reloadData];
		self.eduHeight.constant = self.eduTableView.contentSize.height;
		//[self updateViewConstraints];
	}];
}

- (void)addWorkExp:(UIButton *)sender
{
	AddWorkExpViewController *vc = [[AddWorkExpViewController alloc] init];
	vc.resumeId = self.resumeId;
	NSLog(@"the resumeID is %@", vc.resumeId);
	[self.navigationController pushViewController:vc animated:YES];
}

- (void)addEduExp:(UIButton *)sender
{
	AddEduExpViewController *vc = [[AddEduExpViewController alloc] init];
	vc.resumeId = self.resumeId;
	NSLog(@"the resumeID is %@", vc.resumeId);
	[self.navigationController pushViewController:vc animated:YES];
}

- (IBAction)editWantJob:(id)sender {
	EditWantJobViewController *vc = [[EditWantJobViewController alloc] init];
	vc.model = self.resumeInfo;
	[self.navigationController pushViewController:vc animated:YES];
}

- (IBAction)editResumeTitle:(id)sender {
	ZYInputAlertView *alertView = [ZYInputAlertView alertView];
	alertView.inputTextView.text = self.resumeInfo.title;
	[alertView confirmBtnClickBlock:^(NSString *inputString) {
		[self updateResumeInfoForType:0 param:inputString];
	}];
	[alertView show];
}

- (IBAction)editAdvantage:(id)sender {
	ZYInputAlertView *alertView = [ZYInputAlertView alertView];
	alertView.inputTextView.text = self.resumeInfo.superiority;
	[alertView confirmBtnClickBlock:^(NSString *inputString) {
		[self updateResumeInfoForType:1 param:inputString];
	}];
	[alertView show];
}
- (IBAction)editBasicInfo:(id)sender {
	EditResumeBasicInfoViewController *vc = [[EditResumeBasicInfoViewController alloc] init];
	vc.resumeInfo = self.resumeInfo;
	[self.navigationController pushViewController:vc animated:YES];
}

- (IBAction)changeHeadImg:(id)sender {
	TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:1 columnNumber:4 delegate:nil pushPhotoPickerVc:YES];
	
	//不展示相册中的视频
	imagePickerVc.allowPickingVideo = NO;
	//展示图片
	imagePickerVc.allowPickingImage = YES;
	//不显示原图选项
	imagePickerVc.allowPickingOriginalPhoto = NO;
	//按时间排序
	imagePickerVc.sortAscendingByModificationDate = YES;
	
	[imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
		[[NetworkTool sharedTool] uploadImage:photos[0] callBack:^(id responseObject) {
			NSArray *imgArr = responseObject[@"data"][@"images"];
			NSString *imgStr = imgArr[0];
			[self updateResumeInfoForType:2 param:imgStr];
		}];
	}];
	[self presentViewController:imagePickerVc animated:YES completion:nil];
}


- (void)updateResumeInfoForType:(NSInteger)type param:(NSString *)param {
	
	NSDictionary *paramDict;
	//更新简历标题
	if (type == 0) {
		paramDict = @{
					  @"resume_id":self.resumeInfo.resumeId,
					  @"title":param,
					  @"name":self.resumeInfo.name,
					  @"head_img":self.resumeInfo.head_img,
					  @"category_id":self.resumeInfo.category_id,
					  @"salary":self.resumeInfo.salary,
					  @"education":self.resumeInfo.education,
					  @"mobile":self.resumeInfo.mobile,
					  @"email":self.resumeInfo.email,
					  @"work_life":self.resumeInfo.work_life,
					  };
	}
	//更新个人优势
	else if(type == 1){
		paramDict = @{
					  @"resume_id":self.resumeInfo.resumeId,
					  @"title":self.resumeInfo.title,
					  @"name":self.resumeInfo.name,
					  @"head_img":self.resumeInfo.head_img,
					  @"category_id":self.resumeInfo.category_id,
					  @"salary":self.resumeInfo.salary,
					  @"education":self.resumeInfo.education,
					  @"mobile":self.resumeInfo.mobile,
					  @"email":self.resumeInfo.email,
					  @"work_life":self.resumeInfo.work_life,
					  @"superiority":param
					  };
	}
	else {
		paramDict = @{
					  @"resume_id":self.resumeInfo.resumeId,
					  @"title":self.resumeInfo.title,
					  @"name":self.resumeInfo.name,
					  @"head_img":param,
					  @"category_id":self.resumeInfo.category_id,
					  @"salary":self.resumeInfo.salary,
					  @"education":self.resumeInfo.education,
					  @"mobile":self.resumeInfo.mobile,
					  @"email":self.resumeInfo.email,
					  @"work_life":self.resumeInfo.work_life,
					  @"superiority":self.resumeInfo.superiority
					  };
	}
	
	
	[[NetworkTool sharedTool] requestWithName:@"更新简历" URLString:APIUpdateResume parameters:paramDict method:@"POST" callBack:^(id responseObject) {
		NSString *resultState = responseObject[@"data"][@"status"];
		NSString *message = responseObject[@"data"][@"message"];
		if ([resultState integerValue] == 1) {
			UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"修改成功" message:nil preferredStyle:UIAlertControllerStyleAlert];
			[alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
				[self getResumeInfo];
			}]];
			[self presentViewController:alert animated:YES completion:nil];
		}
		else {
			[self presentViewController:[LZCTools alertMessage:@"您没有修改任何东西"] animated:YES completion:nil];
		}
	}];
}

- (ResumeInfo *)resumeInfo
{
	if (!_resumeInfo) {
		_resumeInfo = [[ResumeInfo alloc] init];
	}
	return _resumeInfo;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
